package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.service.StudentService;
import com.itheima.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //设置请求和响应编码
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            //获取方法名
            String methodName = req.getParameter("method");
            //获取字节码对象
            Class clazz = this.getClass();
            //调用getMethod
            Method method = clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //调用方法
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void selectByPage(HttpServletRequest request, HttpServletResponse response) {
        try {
            //获取请求参数
            String currentPage = request.getParameter("currentPage");
            String pageSize = request.getParameter("pageSize");
            //调用service
            PageInfo<Student> pageInfo = studentService.selectByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
            //转json
            new ObjectMapper().writeValue(response.getWriter(), pageInfo);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addStu(HttpServletRequest request, HttpServletResponse response) {
        //获取请求参数集合
        Map<String, String[]> map = request.getParameterMap();
        //封装数据
        Student student = new Student();
        dateConvert();
        try {
            BeanUtils.populate(student, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用service
        studentService.addStu(student);
    }

    public void dateConvert() {
        ConvertUtils.register(new Converter() {
            public Object convert(Class type, Object value) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return simpleDateFormat.parse(value.toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }, Date.class);
    }

    public void deleteStu(HttpServletRequest request, HttpServletResponse response) {
        //获取删除的学号
        String number = request.getParameter("number");
        //调用service
        studentService.deleteStu(number);
    }

    public void updateStu(HttpServletRequest request, HttpServletResponse response) {
        //获取请求参数集合
        Map<String, String[]> map = request.getParameterMap();
        //封装数据
        Student student = new Student();
        dateConvert();
        try {
            BeanUtils.populate(student, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用service
        studentService.updateStu(student);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
